package com.slt.android.customview.d2canvas;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Layer的意思是图层
 * Created by dp on 2017/12/25.
 */

public class LayerView extends View {
    Paint mPaint;
    public LayerView(Context context) {
        super(context);
        init(context);
    }

    public LayerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LayerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mPaint = new Paint();
        mPaint.setColor(Color.BLUE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);
        canvas.drawCircle(150 , 150 , 100 , mPaint);
        //saveLayerAlpha将一个图层入栈，之后的操作都在这个图层上进行
        canvas.saveLayerAlpha(0 , 0 , 400 , 400 , 100 , Canvas.CLIP_TO_LAYER_SAVE_FLAG);
        mPaint.setColor(Color.RED);
        canvas.drawCircle(200 , 200 , 100 , mPaint);
        //restore将一个图层出栈，把图像绘制到cavas上
        canvas.restore();
    }
}
